package com.seu.me.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seu.me.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liangfeihu on 2018/4/2.
 */
@Slf4j
public class MyTest4 {

    public static void main(String[] args) throws Exception {
        /*RestTemplate restTemplate = new RestTemplate();
        JSONObject param = new JSONObject();
        param.put("bank_no", "COMM");
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<Map> entity = new HttpEntity<>(param, headers);
        JSONObject object = restTemplate.postForObject("http://47.95.43.216:5000/judge", entity, JSONObject.class );
        System.out.println(JSONObject.toJSONString(object));
        System.out.println(object.getString("certType"));*/

        /*JSONObject param = new JSONObject();
        param.put("userId", 654321);
        param.put("cardNo", "6217001210048999278");
        param.put("application", "haoxin.report");
        param.put("ApplicationId", 123456);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<Map> entity = new HttpEntity<>(param, headers);
        String url = "http://new-hx-test.c2a726fac09544bec80ef0cdd3999891d.cn-beijing.alicontainer.com/rules";
        //JSONObject object = restTemplate.postForObject(url, entity, JSONObject.class );
        String object = restTemplate.postForObject(url, entity, String.class );
        log.info("[生成风控报告内容]return={}", object);*/

        RestTemplate restTemplate = new RestTemplate();

        String appId = "K7D432G7DF5SA9V";
        String aeskey = "AmCILBEADCgkHhDK";
        String url = "http://106.15.126.217/beadwalletloanapp/sxyDrainage/xjbs/checkUser.do";
        // 业务请求参数处理
        Map<String, Object> map = new HashMap<>();
        map.put("name", "梁飞虎");
        map.put("idCard", "130425198710225816");
        map.put("phone", "15214353029");
        // request 业务参数加密
        String request = AESUtil.encrypt(JSON.toJSONString(map), aeskey);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("appId", appId);
        params.add("request", request);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, JSONObject.class);
        System.out.println(exchange.getBody());
        String response = AESUtil.decrypt(exchange.getBody().getString("response"), aeskey);
        System.out.println(response);
        JSONObject parseObject = JSONObject.parseObject(response);
        System.out.println(parseObject.getInteger("isStock") + ":" + parseObject.getInteger("isCanLoan"));
    }
}
